import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
  OneToMany,
} from 'typeorm';
import { User } from '../../user/entities/user.entity';
import { Team } from '../../team/entities/team.entity';

@Entity('todos')
export class Todo {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ type: 'varchar', length: 500 })
  title: string;

  @Column({ type: 'boolean', default: false })
  completed: boolean;

  @Column({ name: 'creator_id', nullable: true })
  creatorId: number;

  @Column({ name: 'assignee_id', nullable: true })
  assigneeId: number;

  @Column({ name: 'parent_id', nullable: true })
  parentId: number;

  @Column({ name: 'team_id', nullable: true })
  teamId: number;

  @Column({ name: 'due_date', type: 'timestamp', nullable: true })
  dueDate: Date;

  @ManyToOne(() => User, { eager: false })
  @JoinColumn({ name: 'creator_id' })
  creator: User;

  @ManyToOne(() => User, { eager: false })
  @JoinColumn({ name: 'assignee_id' })
  assignee: User;

  @ManyToOne(() => Todo, (todo) => todo.children, { nullable: true })
  @JoinColumn({ name: 'parent_id' })
  parent: Todo;

  @OneToMany(() => Todo, (todo) => todo.parent)
  children: Todo[];

  @ManyToOne(() => Team, (team) => team.todos, { nullable: true })
  @JoinColumn({ name: 'team_id' })
  team: Team;

  @CreateDateColumn({ name: 'created_at' })
  createdAt: Date;

  @UpdateDateColumn({ name: 'updated_at' })
  updatedAt: Date;
}
